package com.seu.project.seuquiz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seu.project.seuquiz.common.ErrorCode;
import com.seu.project.seuquiz.exception.BusinessException;
import com.seu.project.seuquiz.mapper.UserQuestionMapMapper;
import com.seu.project.seuquiz.model.dto.userQuestionMap.UserQuestionMapQueryRequest;
import com.seu.project.seuquiz.model.entity.UserQuestionMap;
import com.seu.project.seuquiz.service.QuestionService;
import com.seu.project.seuquiz.service.UserQuestionMapService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author lenovo
* @description 针对表【user_question_map(用户-题目map表)】的数据库操作Service实现
* @createDate 2023-07-15 13:47:58
*/
@Slf4j
@Service
public class UserQuestionMapServiceImpl extends ServiceImpl<UserQuestionMapMapper, UserQuestionMap>
    implements UserQuestionMapService {

    @Resource
    private UserQuestionMapMapper userQuestionMapMapper;

    @Resource
    private QuestionService questionService;

    @Override
    public long userQuestionMapAdd(String studentId, String questionId, boolean isRight) {
        if (StringUtils.isAnyBlank(studentId, questionId)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "学号、题目号不能为空");
        }

        synchronized (studentId + questionId) {
            UserQuestionMap userQuestionMap = new UserQuestionMap();
            // todo: 存在执行更新操作、否则插入
            userQuestionMap.setStudentId(studentId);
            userQuestionMap.setQuestionId(questionId);

            QueryWrapper<UserQuestionMap> queryWrapper = getQueryWrapper(UserQuestionMapQueryRequest.builder()
                    .studentId(studentId)
                    .questionId(questionId)
                    .build()
            );

            UserQuestionMap dbExist = getOne(queryWrapper);
            if (dbExist == null) {
                // 不存在执行插入操作
                boolean save = this.save(userQuestionMap);
                if(!save){
                    throw new BusinessException(ErrorCode.SYSTEM_ERROR,"添加失败，数据库错误");
                }
                return userQuestionMap.getId();
            } else {
                // 存在则执行更新操作
                dbExist.setTotalCnt(dbExist.getTotalCnt() + 1);
                if (isRight) {
                    dbExist.setCorrectCnt(dbExist.getCorrectCnt() + 1);
                }
                boolean save = this.updateById(dbExist);
                if(!save){
                    throw new BusinessException(ErrorCode.SYSTEM_ERROR,"添加失败，数据库错误");
                }

                // 用户做题后，更新题目做的总次数和正确次数
                boolean b = questionService.questionUserAdd(Integer.parseInt(questionId), isRight);
                if(!b){
                    throw new BusinessException(ErrorCode.SYSTEM_ERROR,"添加失败，数据库错误");
                }
                return dbExist.getId();
            }
        }
    }

    @Override
    public QueryWrapper<UserQuestionMap> getQueryWrapper(UserQuestionMapQueryRequest request) {
        QueryWrapper<UserQuestionMap> queryWrapper = new QueryWrapper<>();
        if (request == null) {
            return null;
        }

        Long id = request.getId();
        List<Long> ids = request.getIds();
        String studentId = request.getStudentId();
        String questionId = request.getQuestionId();
        List<String> questionIds = request.getQuestionIds();
        Integer correctCnt = request.getCorrectCnt();
        Integer totalCnt = request.getTotalCnt();
        String sortField = request.getSortField();
        String sortOrder = request.getSortOrder();

        queryWrapper.eq(id != null && id > 0,"id", id);
        queryWrapper.in(ids != null && ids.size() > 0,"id");
        queryWrapper.eq(StringUtils.isNotBlank(studentId), "studentID", studentId);
        queryWrapper.eq(StringUtils.isNotBlank(questionId), "questionID", questionId);
        queryWrapper.in(questionIds != null && questionIds.size() > 0,"questionID", questionIds);
        queryWrapper.ge(correctCnt != null && correctCnt > 0,"correctCnt", correctCnt);
        queryWrapper.ge(totalCnt != null && totalCnt > 0,"totalCnt", totalCnt);
//        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
//                sortField);
        return queryWrapper;
    }
}




